<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * Class AdminManageController
 * @package Admin\Controller
 * 管理员管理
 */
class AdminManageController extends Controller {

    private $model_name = 'Admin';
    public function index(){
        $this->display();
    }

    public function get_all(){
        $list = get_date_grid($this->model_name);
        echo json_encode($list);
    }

    public function add(){
        $role_model = \gt_base::load_model('Role');
        $role_list = $role_model->getAll();
        $this->assign('roles', $role_list);
        $this->display();
    }

    public function add_admin(){
        if(IS_POST){
            $admin_model = \gt_base::load_model('Admin');
            $post_data = filterInput($_POST);
            if(!$this->isset_pwd($post_data)){
                unset($post_data['password']);
                unset($post_data['pwdconfirm']);
            }
            $post_data['password'] = md5(md5($post_data['password']));
            $result = $admin_model->addObj($post_data, 'username');
            if($result){
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }
    }

    public function check_username(){
        $get_data = filterInput($_GET);
        $admin_model = \gt_base::load_model("Admin");
        $map['username'] = array('eq', $get_data['username']);
        $one = $admin_model->check_user($map);
        if(!$one){
            $return['status'] = 0;
        }else{
            $return['status'] = 1;
        }
        echo json_encode($return);
    }

    public function edit(){
        $get_data = filterInput($_GET);
        if(!isset($get_data['id'])){
            echo 'no parm id';
            return;
        }
        $admin_model = \gt_base::load_model('Admin');
        $admin = $admin_model->queryObj($get_data['id'], 'id');
        $this->assign('admin', $admin);
        $role_model = \gt_base::load_model('Role');
        $role_list = $role_model->getAll();
        $this->assign('roles', $role_list);
        $this->display();
    }

    public function edit_admin(){
        if(IS_POST){
            $post_data = filterInput($_POST);
            if(!$this->isset_pwd($post_data)){
                unset($post_data['password']);
                unset($post_data['pwdconfirm']);
            }
            if($this->isset_pwd($post_data)){
                unset($post_data['pwdconfirm']);
            }
            $post_data['password'] = md5(md5($post_data['password']));
            $admin_model = \gt_base::load_model('Admin');
            $result = $admin_model->updateObj($post_data);
            if($result){
                $this->success('更改成功');
            }else{
                $this->error('更改失败');
            }
        }
    }

    private function isset_pwd($data){
        if(!isset($data['password'])||!isset($data['pwdconfirm'])){
            return false;
        }
        if($data['password'] != $data['pwdconfirm']){
            return false;
        }
        return true;
    }

    public function del(){
        $data = filterInput($_POST);
        $ids = $data['ids'];
        $model = D($this->model_name);
        $result = $model->deleteArray($ids);
        if($result){
            echo 1;
        }else{
            echo 0;
        }
    }

}